home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12612 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  117 lines

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: Philip Staite <pstaite+@rchland.ibm.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ problem with constructor.
  5. Date: Wed, 20 Mar 1996 15:16:31 -0600
  6. Organization: IBM Rochester, MN
  7. Message-ID: <315075AF.2781@rchland.ibm.com>
  8. References: <DoGGGp.Muy@latcs1.lat.oz.au>
  9. NNTP-Posting-Host: powertool.rchland.ibm.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; AIX 1)
  14.  
  15. Gregary J Boyles wrote:
  16.  
  17. Basically the problem is you never get storage for your strings.  This
  18. comes down to fundamental string handling.  You can do this over and
  19. over again in every class you design that uses strings :-(  Or you can
  20. use a string class and merely use string objects.  To deomonstrate this,
  21. I've modified your code with a simple buffer class that encapsulates
  22. string memory management.  This code is just hacked out off the top of
  23. my head, but should be pretty close.
  24.  
  25.  // address.cpp
  26.  
  27.  #include "address.h"
  28.  #include <string.h>
  29.  
  30.  // Constructors
  31.  Address::Address(int ANumber,const char *AStreet,const char *ACity,int
  32. AZip)
  33.  {
  34.       Number=ANumber;
  35.        Street = AStreet;
  36.        City = ACity;
  37.       Zip=AZip;
  38.  }
  39.  
  40.  Address::Address()
  41.  {
  42.       Number=0;
  43.  
  44.  
  45.       Zip=0;
  46.  }
  47. > // Copy constructor
  48. > Address::Address(Address& AnAddress)
  49. > {
  50. >      Number=AnAddress.Number;
  51.        Street = AnAddress.Street;
  52.        City = AnAddress.City;
  53. >      Zip=AnAddress.Zip;
  54. > }
  55. > // Deconstructor
  56. > Address::~Address()
  57. > {
  58. >      Number=0;
  59.  
  60.  
  61. >      Zip=0;
  62. > }
  63. > // address.h
  64. > #ifndef __ADDRESS_H
  65. > #define __ADDRESS_H
  66. > #define STR_STREET " street"
  67. > #include "defines.h"
  68.  
  69. char* dupe( const char* p ) {
  70.     if( ! p )
  71.         return 0;
  72.     return strcpy( new char[ strlen( p ) + 1 ], p ); }
  73.  
  74.  
  75. class buf {
  76.   public:
  77.     buf( const char* s = 0 ) : p( dupe( s ) ) {}
  78.     buf( const buf& b ) : p( dupe( b.p ) ) {}
  79.     ~buf() { delete[] p; }
  80.     buf& operator=( const buf& b ) { delete[] p; p = dupe( b.p ); return
  81. *this; }
  82.     buf& operator=( const char* s ) { delete[] p; p = dupe( s ); return
  83. *this; }
  84.     operator char*() { return p; }
  85.     operator const char*() const { return p; }
  86.   private:
  87.     char* p;
  88. };
  89.  
  90. > class Address
  91. > {
  92. >      private : int Number;
  93.                  buf Street,
  94.                      City;
  95. >                int Zip;
  96. >      public : // Constructors
  97. >               Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  98. >               Address();
  99. >               // Copy constructor
  100. >               Address(Address& AnAddress);
  101. >               // Deconstructor
  102. >               ~Address();
  103. > };
  104. > #endif
  105.  
  106. -- 
  107.  
  108. Phil Staite, (507) 253-2529, team OS/2
  109. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  110.